package com.powerunion.datacollection.service;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import com.powerunion.datacollection.common.SystemException;
import com.powerunion.datacollection.dao.AccessoriesDao;
import com.powerunion.datacollection.dao.DaoException;
import com.powerunion.datacollection.dao.DaoFactory;
import com.powerunion.datacollection.model.Accessories;
import com.powerunion.util.log.LogHelper;

public class AccessoriesManager {
	private static Logger logger = LogHelper.getInstance().getLogger(
			AccessoriesManager.class);
	private AccessoriesDao dao = null;

	public AccessoriesManager() {
		super();
		this.dao = (AccessoriesDao) DaoFactory.getInstance().getDao(
				AccessoriesDao.class);
	}

	public boolean save(Accessories accessories, Connection conn)
			throws Exception {
		// 参数为空
		if (accessories == null) {
			String[] args = { "accessories" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter accessories NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		return this.dao.save(accessories, conn);

	}
	
	public boolean save(Accessories accessories) throws Exception {
		// 参数为空
		if (accessories == null) {
			String[] args = { "accessories" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter accessories NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		return this.dao.save(accessories);
	}



	public Accessories findAccessoriesByKey(String id) throws Exception {
		// 参数为空
		if (StringUtils.isEmpty(id)) {
			String[] args = { "id" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter id NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}

		return this.dao.findAccessoriesByKey(id);

	}

	public String getAccessoriesByFormId(String formId) throws DaoException {
		// 参数为空
		if (StringUtils.isEmpty(formId)) {
			String[] args = { "formId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter formId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}

		List list = this.dao.getAccessoriesByFormId(formId);
		JSONArray jsonArr = JSONArray.fromObject(list);
		String result = "{results:" + jsonArr + "}";
		return result;
	}

	public String getAccessoriesBySubmi(String formId, String submiUser,
			String procId, String actId) throws Exception {
		// 参数为空
		if (StringUtils.isEmpty(formId)) {
			String[] args = { "formId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter formId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		// 参数为空
		if (StringUtils.isEmpty(submiUser)) {
			String[] args = { "submiUser" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter submiUser NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		// 参数为空
		if (StringUtils.isEmpty(procId)) {
			String[] args = { "procId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter procId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		// 参数为空
		if (StringUtils.isEmpty(actId)) {
			String[] args = { "actId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter actId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		List list = this.dao.getAccessoriesBySubmi(formId, submiUser, procId,
				actId);
		JSONArray jsonArr = JSONArray.fromObject(list);
		String result = "{results:" + jsonArr + "}";
		return result;
	}

	public String getAccessories4SubWork(String formId, String procId,
			String approveId) throws Exception {
		// 参数为空
		if (StringUtils.isEmpty(formId)) {
			String[] args = { "formId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter formId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}

		// 参数为空
		if (StringUtils.isEmpty(procId)) {
			String[] args = { "procId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter procId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		// 参数为空
		if (StringUtils.isEmpty(approveId)) {
			String[] args = { "approveId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter approveId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		List list = this.dao.getAccessories4SubWork(formId, procId, approveId);
		JSONArray jsonArr = JSONArray.fromObject(list);
		String result = "{results:" + jsonArr + "}";
		return result;
	}

	public String getAccessories4AllSubWork(String formId, String procId,
			String actId) throws Exception {
		// 参数为空
		if (StringUtils.isEmpty(formId)) {
			String[] args = { "formId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter formId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}

		// 参数为空
		if (StringUtils.isEmpty(procId)) {
			String[] args = { "procId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter procId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		// 参数为空
		if (StringUtils.isEmpty(actId)) {
			String[] args = { "actId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter actId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}

		SubmissionWorkItemManager swm = new SubmissionWorkItemManager();
		List submissionWorkItems = new ArrayList();
		submissionWorkItems = swm.getAllSubWorksByAct(procId, actId, 2);

		List list = this.dao.getAccessories4AllSubWork(formId, procId,
				submissionWorkItems);
		JSONArray jsonArr = JSONArray.fromObject(list);
		String result = "{results:" + jsonArr + "}";
		return result;
	}

	public boolean delete(String[] codes) throws DaoException {
		// 参数为空
		if (codes == null || codes.length < 1) {
			String[] args = { "codes" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter codes NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		return this.dao.delete(codes);
	}

}
